import router from '@/router'
import axios from 'axios'

//**做了，统一的地址地址配置
// **请求拦截器--统一处理的token--设置了请求头的token

const http = axios.create({
  // baseURL: 'http://localhost:3001',
  baseURL: import.meta.env.VITE_API_BASE_URL,
  timeout: 60000,
})

// 添加请求拦截器
http.interceptors.request.use(
  function (config) {
    const token: string = localStorage.getItem('xx-mall-admin-token') as string
    if (token) {
      config['headers']['token'] = token
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  },
)
//相应拦截器
http.interceptors.response.use(
  function (response){
if (response.data.code==10119) {
  localStorage.removeItem('xx-mall-admin-token')
  router.push('/login')
}
    return response
  }
)
// let http = axios.create({
//   baseURL: 'http://localhost:3001',
//   timeout: 60000,
// })
export default http
